package com.example.blog.controller;
import com.example.blog.model.Article;
import com.example.blog.model.User;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("user")

public class UserController extends  BaseController {
    private SqlSession sqlSession;

    public UserController(SqlSession sqlSession) {
        this.sqlSession = sqlSession;
    }
    @RequestMapping("signIn")
    private String signIn(User user) {
        user = sqlSession.selectOne("user.queryUserByUsernameAndPassword", user);
        if (user == null) {
            request.setAttribute("error", "Invalid username or password.");
            return "/index.jsp";
        } else {
            user = sqlSession.selectOne("user.queryUserArticlesById", user.getId());
            session.setAttribute("user", user);
            return "redirect:/";
        }
    }
    @RequestMapping("signUp")
    private String signUp(User user){
        sqlSession.insert("user.create",user);

        session.setAttribute("id", user.getId());
        return "redirect:/index.jsp";

    }
    @RequestMapping(value = "signOut" ,method = RequestMethod.GET)
    private String signOut() {

        session.invalidate();

        return "redirect:/index.jsp";
    }
}
